package cn.da.shuai.mq.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String DEFAULT_EXCHANGE = "cool.default.exchange";
    public static final String DEFAULT_QUEUE = "cool.default.queue";
    public static final String DEFAULT_ROUTING_KEY = "cool.default.key";

    @Bean
    public DirectExchange defaultExchange() {
        return new DirectExchange(DEFAULT_EXCHANGE);
    }

    @Bean
    public Queue defaultQueue() {
        return QueueBuilder.durable(DEFAULT_QUEUE).build();
    }

    @Bean
    public Binding binding(Queue defaultQueue, DirectExchange defaultExchange) {
        return BindingBuilder.bind(defaultQueue)
                .to(defaultExchange)
                .with(DEFAULT_ROUTING_KEY);
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }
}